package com.ocean.web.controller.product;

import java.io.IOException;
import java.util.List;

import com.ocean.common.utils.DateUtils;
import com.ocean.common.utils.MyClassLoader;
import com.ocean.common.utils.StringUtils;
import com.ocean.common.utils.file.FileUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.product.domain.TSDocument;
import com.ocean.product.service.ITSDocumentService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 文档下载,新闻,法规Controller
 *
 * @author ocean
 * @date 2023-02-06
 */
@Controller
@RequestMapping("/product/document")
public class TSDocumentController extends BaseController {
    private String prefix = "product/document";

    @Autowired
    private ITSDocumentService tSDocumentService;

    @RequiresPermissions("product:document:view")
    @GetMapping()
    public String document() {
        return prefix + "/document";
    }

    /**
     * 查询文档下载,新闻,法规列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSDocument tSDocument) {
        startPage();
        List<TSDocument> list = tSDocumentService.selectTSDocumentList(tSDocument);
        return getDataTable(list);
    }

    /**
     * 导出文档下载,新闻,法规列表
     */
    @Log(title = "文档下载,新闻,法规", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSDocument tSDocument) {
        List<TSDocument> list = tSDocumentService.selectTSDocumentList(tSDocument);
        ExcelUtil<TSDocument> util = new ExcelUtil<TSDocument>(TSDocument.class);
        return util.exportExcel(list, "文档下载,新闻,法规数据");
    }

    /**
     * 新增文档下载,新闻,法规
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文档下载,新闻,法规
     */
    @Log(title = "文档下载,新闻,法规", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TSDocument tSDocument) {
        return toAjax(tSDocumentService.insertTSDocument(tSDocument));
    }

    /**
     * 修改文档下载,新闻,法规
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TSDocument tSDocument = tSDocumentService.selectTSDocumentById(id);
        mmap.put("tSDocument", tSDocument);
        return prefix + "/edit";
    }

    /**
     * 修改保存文档下载,新闻,法规
     */
    @Log(title = "文档下载,新闻,法规", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSDocument tSDocument) {
        return toAjax(tSDocumentService.updateTSDocument(tSDocument));
    }

    /**
     * 删除文档下载,新闻,法规
     */
    @Log(title = "文档下载,新闻,法规", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tSDocumentService.deleteTSDocumentByIds(ids));
    }

    /**
     * 文件录入——跳转
     *
     * @param businesskey 产品id
     * @param mmap
     * @return
     */
    @GetMapping("upload")
    public String upload(@RequestParam String businesskey, ModelMap mmap) {
        mmap.put("businesskey", businesskey);
        return "/product/product/upload";
    }

    /**
     * 文件录入——保存
     *
     * @param document
     * @return
     */
    @PostMapping("upload")
    @ResponseBody
    public AjaxResult upload(TSDocument document) {
        int i = tSDocumentService.upload(document);
        return toAjax(i);
    }

    @GetMapping("download")
    @ResponseBody
    public void download(@RequestParam String id, HttpServletResponse response) {
        try {
            TSDocument tsDocument = tSDocumentService.selectTSDocumentById(id);
            if (!FileUtils.checkAllowDownload(tsDocument.getFilePath())) {
                throw new Exception(StringUtils.format("文件名称({})非法，不允许下载。 ", tsDocument.getAttachmenttitle()));
            }
            FileUtils.setAttachmentResponseHeader(response, tsDocument.getAttachmenttitle()+"."+tsDocument.getExtend());
            FileUtils.writeBytes(tsDocument.getFilePath(), response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
